package com.bobi.tfca.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bobi.tfca.model.constant.RedisConstant;
import com.bobi.tfca.model.constant.ResourcePathConstant;
import com.bobi.tfca.model.entity.SysResourceEntity;
import com.bobi.tfca.model.enums.SysResourceCatEnum;
import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.service.SysResourceService;
import com.bobi.tfca.utils.CosUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 李文发
 * @date 2024/4/15
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源接口")
@Slf4j
public class ResourceController {

    @Autowired
    private SysResourceService sysResourceService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "上传图片")
    @PostMapping("/uploadImage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "图片")
    })
    public R<String> uploadImage(@RequestPart("file") MultipartFile file) {
        try {
            log.info("正在上传图片");
            String url = "";
            url = CosUtils.uploadFile(file, ResourcePathConstant.IMAGE_PATH);
            log.info("上传成功");
            return R.data(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "查询会员权益")
    @GetMapping("/getMemberRights")
    public R<String> getMemberRights() {
        try {
            String value = stringRedisTemplate.opsForValue().get(RedisConstant.MEMBER_RIGHTS);
            if (value != null) {
                return R.data(value);
            }
            String rights = "";
            SysResourceEntity resource = sysResourceService.getOne(new LambdaQueryWrapper<SysResourceEntity>()
                    .eq(SysResourceEntity::getCategory, SysResourceCatEnum.MEMBER_RIGHTS.getCode()));
            rights = resource.getPlainText();
            stringRedisTemplate.opsForValue().set(RedisConstant.MEMBER_RIGHTS, rights);
            return R.data(rights);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "查询协会章程")
    @GetMapping("/getAssRules")
    public R<String> getAssRules() {
        String rights = "";
        try {
            String value = stringRedisTemplate.opsForValue().get(RedisConstant.ASS_RULES);
            if (value != null) {
                return R.data(value);
            }
            SysResourceEntity resource = sysResourceService.getOne(new LambdaQueryWrapper<SysResourceEntity>()
                    .eq(SysResourceEntity::getCategory, SysResourceCatEnum.ASS_RULES.getCode()));
            rights = resource.getPlainText();
            stringRedisTemplate.opsForValue().set(RedisConstant.ASS_RULES, rights);
            return R.data(rights);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "保存会员权益")
    @PostMapping("/saveRightsChange")
    @ApiImplicitParams({@ApiImplicitParam(name = "change", value = "改变")})
    public R<String> saveRightsChange(@RequestBody String change) {
        boolean update = sysResourceService.update(new LambdaUpdateWrapper<SysResourceEntity>()
                .eq(SysResourceEntity::getCategory, SysResourceCatEnum.MEMBER_RIGHTS.getCode())
                .set(SysResourceEntity::getPlainText, change));
        stringRedisTemplate.delete(RedisConstant.MEMBER_RIGHTS);
        return R.success("保存成功！");
    }

    @ApiOperation(value = "保存协会章程")
    @PostMapping("/saveAssRulesChange")
    @ApiImplicitParams({@ApiImplicitParam(name = "change", value = "改变")})
    public R<String> saveAssRulesChange(@RequestBody String change) {
        boolean update = sysResourceService.update(new LambdaUpdateWrapper<SysResourceEntity>()
                .eq(SysResourceEntity::getCategory, SysResourceCatEnum.ASS_RULES.getCode())
                .set(SysResourceEntity::getPlainText, change));
        stringRedisTemplate.delete(RedisConstant.ASS_RULES);
        return R.success("保存成功！");
    }

    @ApiOperation(value = "发送邮箱验证码给已注册用户")
    @PostMapping("/sendEmailCode2Exist/{account}")
    @ApiImplicitParams({@ApiImplicitParam(name = "account", value = "账号")})
    public R<String> sendEmailCode(@PathVariable("account") String account) {
        Boolean success = sysResourceService.sendEmailCode2Exist(account);
        return R.success("发送成功");
    }

    @ApiOperation(value = "发送邮箱验证码给未注册用户")
    @PostMapping("/sendEmailCode4Register/{account}")
    @ApiImplicitParams({@ApiImplicitParam(name = "account", value = "账号"),
            @ApiImplicitParam(name = "email", value = "邮箱")})
    public R<String> sendEmailCode(@PathVariable("account") String account, @RequestParam("email") String email) {
        Boolean success = sysResourceService.sendEmailCode4Register(account, email);
        return success ? R.success("发送成功") : R.failed("发送验证码失败，不存在的邮箱！");
    }

    @ApiOperation(value = "上传组织架构图片")
    @PostMapping("/saveOrgStructurePicture")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "图片")
    })
    public R<String> saveOrgStructurePicture(@RequestPart("file") MultipartFile file) {
        String url = "";
        try {
            url = CosUtils.uploadFile(file, ResourcePathConstant.IMAGE_PATH);
            boolean update = sysResourceService.update(new LambdaUpdateWrapper<SysResourceEntity>()
                    .eq(SysResourceEntity::getCategory, SysResourceCatEnum.ORG_STRUCTURE.getCode())
                    .set(SysResourceEntity::getPath, url));
            stringRedisTemplate.delete(RedisConstant.ORG_STRUCTURE);
            return R.data(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "查询协会架构图片")
    @GetMapping("/getOrgStructurePicture")
    public R<String> getOrgStructurePicture() {
        String url = "";
        try {
            String value = stringRedisTemplate.opsForValue().get(RedisConstant.ORG_STRUCTURE);
            if (value != null) {
                return R.data(value);
            }
            SysResourceEntity resource = sysResourceService.getOne(new LambdaQueryWrapper<SysResourceEntity>()
                    .eq(SysResourceEntity::getCategory, SysResourceCatEnum.ORG_STRUCTURE.getCode()));
            url = resource.getPath();
            stringRedisTemplate.opsForValue().set(RedisConstant.ORG_STRUCTURE, url);
            return R.data(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
